const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.connect('mongodb://localhost:27017/xqmanager', { useNewUrlParser: true, useUnifiedTopology: true });
const managerSchema = new Schema({
  managername: { type: String, default: '' },//有默认值的属性再插入时不添加会自动以默认值插入
  account: { type: String },
  password: { type: String },
  remarks: { contents: { type: String, default: '' }, date: { type: Date, default: Date.now } },//对插入这个数组的每一项对象数据的约束条件


});
const Manager = mongoose.model('manager', managerSchema);


exports.addmanager = function (manager, callback) {
  Manager.find({
    account: manager.account
  }, function (err, res) {
    if (err) {
    }
    else {
      if (res.length == 0) {
        const managerinfo = new Manager({
          managername: manager.managername,
          account: manager.account,
          password: manager.password,
          remarks: { contents: manager.contents },
        })
        managerinfo.save().then(() => {
          callback('500')
        });
      }
      else {
        console.log(res)
        callback('404')
      }
    }
  })

}


exports.search = function (manager, callback) {
  Manager.find({
    account: manager.account,
    password: manager.password
  }, function (err, res) {
    if (err) {
      console.log(err)
    }
    else {
      if (res.length == 0) {
        callback(res, '404')
      }
      else {
        let token = '51522zzwlwlbb'
        callback(res, token)
      }

    }
  })
}

exports.delete = function (account, callback) {
  Manager.remove({
    account
  }, function (err, ret) {
    if (err) {
      console.log('删除失败')
    }
    else {
      callback(ret)
    }
  })
}
